import commonRequest from '/@/api/common/common';

//文件转base64
function fileToBase64(file: any) {
	return new Promise((resolve, reject) => {
		const reader = new FileReader();
		reader.onload = (event: any) => {
			// 文件数据的Base64编码在event.target.result中
			resolve(event.target.result);
		};
		reader.onerror = (error) => {
			reject(error);
		};
		reader.readAsDataURL(file);
	});
}

//多文件上传请求
const uploadFiles = async (id: any, fileList: any[]) => {
	let postFiles = [];
	for (let index = 0; index < fileList.length; index++) {
		postFiles.push(fileList[index].raw);
	}
	console.log('postFiles', postFiles);
	//多文件上传
	if (postFiles.length > 0) {
		let res = await commonRequest.postFilesData({
			sourceType: 'SMS',
			sourceId: id,
			files: postFiles,
		});
		console.log("上传附件结果",res)
	}
};

export { fileToBase64, uploadFiles };
